package com.burgeon.backend.Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description: spring boot 的redis工具类
 */


@Component
public class RedisUtil {


    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取list长度
     *
     * @param k
     */
    public long llen(String k) {
        try {
            ListOperations<String, String> list = redisTemplate.opsForList();
            return list.size(k);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<String> lRange(String k, long l, long l1) {
        try {
            ListOperations<String, String> list = redisTemplate.opsForList();
            return list.range(k, l, l1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 列表获取
     *
     * @param k
     */
    public String lPop(String k) {
        try {
            ListOperations<String, String> list = redisTemplate.opsForList();
            return list.leftPop(k);
        } catch (Exception e) {
           e.printStackTrace();
        }
        return null;
    }

    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void rPush(String k, String v) {
        try {
            ListOperations<String, String> list = redisTemplate.opsForList();

            list.rightPush(k, v);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取所有key
     * @return
     */
    public Set keys(String consumer){
        return redisTemplate.keys(consumer);
    }

    /**
     * 存入list
     * @param key
     * @param data
     * @return
     */
    public boolean lset(final String key, List data) {
        boolean result = false;
        try {
            for (Object datum : data) {
                redisTemplate.opsForList().rightPush(key, String.valueOf(datum));
                   }
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public List lget(final String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }



    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public String get(final String key) {
        Object result = null;
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        if (result == null) {
            return null;
        }
        return result.toString();
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set(key, (String) value);

            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     *  秒
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set(key, (String) value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public boolean hmset(String key, Map<String, String> value) {
        boolean result = false;
        try {

            redisTemplate.opsForHash().putAll(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public Map<Object, Object> hmget(String key) {
        Map<Object, Object> result = null;
        try {
            result = redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }




}
